import { createAsyncThunk } from '@reduxjs/toolkit';
import axios, { AxiosError } from 'axios';
import { API } from '../../helpers/API';

export const fetchCartSlice = createAsyncThunk(
	'cart/fetchCartSlice',
	async (data, thunkAPI) => {
		try {
			const token = thunkAPI.getState().user.token;
			const { data } = await axios.post(`${API}/cart`, {
				headers: { 
					Authorization: 'Bearer $'
				}
			});
			return data;
		} catch (error) {
			if (error instanceof AxiosError) {
				throw new Error(error.response?.data.message);
			}
		}
	}
);